{% extends '::base.html.twig' %} 
{% block module_name %}Ensamblado{% endblock %}
{% block title %}Ensamblado :: Nuevo{% endblock %}

{% block body2 %}
<form action="{{ path('ensamblado_create') }}" method="post" id="formID">
<div class="row">
    <div class="col_7">
        <div class="widget clear_fix">
            <h2>Ensamblado :: Nuevo</h2>
            <div class="widget_inside records_list">
                <div class="form">
                    
                        <div class="clearfix">
                            <label>Marca</label>
                            <div class="input">
                                <input type="text" readonly value="{{ entity.marca }}"/>
                                <input type="hidden" value="{{ entity.id }}" name="id"/>
                            </div>
                        </div>
                        <div class="clearfix">
                            <label>Modelo</label>
                            <div class="input">
                                <input type="text" readonly value="{{ entity.modelo }}"/>
                            </div>
                        </div>
                        <div class="clearfix">
                            <label>Bodega</label>
                            <div class="input">
                                <select name="bodega">
                                    {% for bodega in bodegas %}
                                        <option value="{{bodega.id}}">{{ bodega.nombre }}</option>
                                    {% endfor %}
                                </select>
                            </div>
                        </div>
                        <div class="clearfix">
                            <label>Cantidad a Ensamblar</label>
                            <div class="input">
                                <input type="text" id="cantidadEnsamblar" name="cantidad" class="validate[custom[integer]]"/>
                            </div>
                        </div>
                        <div class="clearfix grey-highlight">
                            <div class="input no-label">
                                <button type="submit" class="button blue">Guardar</button>
                                <a href="{{ path('ensamblado') }}" class="button">Regresar a la Lista</a>
                            </div>
                        </div>

                </div>
            </div>
        </div>
    </div>
    <div class="col_5 last">
        <div class="widget clear_fix">
            <h2>Piezas</h2>
            <div class="widget_inside">
                <table class="regular">
                    <thead>
                        <tr>
                            <th class="hide">Id</th>
                            <th>Marca</th>
                            <th>Modelo</th>
                            <th>Bodega</th>
                            <th>Cantidad</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for pieza in piezas %}
                            <tr>
                                <td class="hide">
                                    <input type="hidden" value="{{ pieza.id }}" name="piezas[{{loop.index}}][idArticulo]" />
                                </td>
                                <td>{{ pieza.marca }}</td>
                                <td>{{ pieza.modelo }}</td>
                                <td>
                                    <select class="bodegas" name="piezas[{{loop.index}}][idBodega]">
                                        <option>- Seleccione -</option>
                                        {% for bodega in bodegas %}
                                            <option value="{{bodega.id}}">{{ bodega.nombre }}</option>
                                        {% endfor %}
                                    </select>
                                </td>
                                <td class="cantidad"></td>
                            </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
</form>
<script>
$(document).ready(function(){
    $("#formID").validationEngine('attach');
    
    var originalsValues;
    var newValues;
    var restable;
    
    updateValues();
    
    $("#cantidadEnsamblar").keyup(function(){
        if($("#cantidadEnsamblar").val() != ""){
            $(".cantidad").each(function(index){
                $(this).text(originalsValues[index]).effect("highlight", {}, 900);;
            });
            newValues = new Array();
            originalsValues.forEach(function(item) { 
                var num = item - parseInt($("#cantidadEnsamblar").val());
                if(num < 0 || isNaN(num)){
                    restable = false;
                }
            });
            if(restable){
                originalsValues.forEach(function(item) { 
                    var num = item - parseInt($("#cantidadEnsamblar").val());
                    newValues.push(num);
                });
                $(".cantidad").each(function(index){
                    if(!isNaN(newValues[index])){
                        $(this).text(newValues[index]).effect("highlight", {}, 900);
                    }else{
                        $(this).text(originalsValues[index]).effect("highlight", {}, 900);
                    }
                });
                console.log("newValues: ", newValues);
            }else{
                alert("No hay Cantidades suficientes para crear este articulo");
                restable = true;
            }
        }else{
            $(".cantidad").each(function(index){
                $(this).text(originalsValues[index]).effect("highlight", {}, 900);
            });
        }
    });
    
    function updateValues(){
        originalsValues = new Array();
        
        $(".cantidad").each(function(){
            originalsValues.push(parseInt($(this).text()));
        });
        
        restable = true;
        
        console.log("originalsValues: ", originalsValues);
    }
    
    $(".bodegas").change(function (e){
        
        $("#cantidadEnsamblar").val("");
        
        var articulo = $(this).parent().parent().children().first().children().val();
        var bodega = $(this).val();
        var cantidad = $(this).parent().parent().children().last();
        
        $.get('{{ path('ensamblado_stock') }}',{idBodega: bodega, idArticulo:  articulo}, function(data){
            
            $(cantidad).text(data).effect("highlight", {}, 900);
            
            updateValues();
        })        
        
    });
});
</script>
{% endblock %}
